<?php $sN = "Question" ?>
<?php
    echo $this->Html->css('jquery-ui-1.8.18.custom');
?>
<style type="text/css">
    .ui-state-highlight{
        margin-top: 10px; 
    }
</style>
<script type="text/javascript">
var offset_cur;
function sort_question(){
    $("#table-content").sortable({
        connectWith: "#table-content",
        placeholder: "ui-state-highlight",
        toleranceElement: 'div',
        start: function(event, ui) { 
            var offset = $(ui.item).height();
            $('.ui-state-highlight').css("height", offset+"px");
        },
        update:function(){
            update_position();
        }
    });
}
function update_position(){
    $.post("<?php echo $this->Html->url(array('controller'=>'surveys','action'=>"update_position",$survey['Survey']['id'])) ?>", {data:$("#table-content").sortable('toArray')}, function(data){
        
    });
}
function ec_all(status){
    if(status == "collapse"){
        $("#table-content .question").each(function(){
            if(!$(this).find('.option-list').hasClass("hidden")){
                $(this).find("a:first").click();
            }
        });
    }else if(status == "expand"){
        $("#table-content .question").each(function(){
            if($(this).find('.option-list').hasClass("hidden")){
                $(this).find("a:first").click();
            }
        });
    }
    return false;
}
$(document).ready(function(){
    sort_question();
    ec_all('collapse');
});
</script>
<script type="text/javascript">
    function add_question(){
        if($("#check_form").html()=="0"){
            url = "<?php echo $this->Html->url(array('action'=>'question_form',$survey_id)) ?>";
            <?php if($survey['Survey']['type']==Survey::EXAMINATION){ ?>
            url = "<?php echo $this->Html->url(array('action'=>'question_form_exam',$survey_id)) ?>";
            <?php } ?>
            $.post(url, function(data){
                $('#table-content').append(data);
                $(".question:last-child").addClass("active-question");
                $("#check_form").html("1");
            });
        }else{
            alert("Please completed other open question first");
        }
        $('html, body').animate({scrollTop: $(document).height()}, 'slow');
        return false;
    }
    function question_ec(id){
        if($('#'+id+' .option-list').hasClass('hidden')){
            $('#'+id+' .option-list').toggleClass("hidden");//removeClass("hidden");
            $('#'+id+' .question-action a:first').text("Collapse");
        }else{
            $('#'+id+' .option-list').toggleClass("hidden");//.addClass("hidden");
            $('#'+id+' .question-action a:first').text("Expand");
        }
        return false;
    }
    function edit_question(id){
        if($("#check_form").html()=="0"){
            url = "<?php echo $this->Html->url(array('action'=>'question_form',$survey_id)) ?>/"+id;
            <?php if($survey['Survey']['type']==Survey::EXAMINATION){ ?>
            url = "<?php echo $this->Html->url(array('action'=>'question_form_exam',$survey_id)) ?>/"+id;
            <?php } ?>
            $.post(url, function(data){
                if(data!='error'){
                    idq = "#question-"+id;
                    $(idq).css("display", "none");
                    $(idq).before(data);
                    $(idq).remove();
                    $("#check_form").html("1");
                    $("form .question").addClass("active-question");
                    sort_question();
                }else{
                    alert("Error!");
                }
            });
        }else{
            alert("Please completed other open question first");
        }
        return false;
    }
    function remove_option(obj){
        $(obj).parents(".option").remove();
        return false;
    }
    function remove_question(id){
        if(confirm("Do you want to remove this question from survey")){
            $.post("<?php echo $this->Html->url(array('action'=>'remove_question',$survey_id)) ?>/"+id, function(data){
                if(data != 'error'){
                    idq = "#question-"+id;
                    $(idq).remove();
                }else{
                    alert("Error!");
                }
            });
        }
        return false;
    }
</script>
<div class="hidden" id="check_form">0</div>
<div id="faqs"></div>
<div id="content">
    <div id="page-heading">
        <h1><?php echo "List question of survey {$survey['Survey']['title']}" ?><?php echo $this->Html->link("Add Question", "#",array('onClick'=>"return add_question();",'class'=>'button')); ?><?php echo $this->Html->link("Add Question From List", array('controller' => "questions", 'action' => 'index',$survey_id, 'admin' => true),array('class'=>'button')); ?><?php echo $this->Html->link("Back to Survey List", array('controller' => $lN, 'action' => 'index', 'admin' => true),array('class'=>'button')); ?></h1>
    </div>
    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
        <tr>
            <th rowspan="3" class="sized">
                <?php echo $this->Html->image('../images/shared/side_shadowleft.jpg', array('width' => '20', 'height' => '300', 'alt' => '')); ?>
            </th>
            <th class="topleft"></th>
            <td id="tbl-border-top">&nbsp;</td>
            <th class="topright"></th>
            <th rowspan="3" class="sized">
                <?php echo $this->Html->image('../images/shared/side_shadowright.jpg', array('width' => '20', 'height' => '300', 'alt' => '')); ?>
            </th>
	</tr>
	<tr>
            <td id="tbl-border-left"></td>
            <td>
                <div id="content-table-inner">
                    <div>
                        <a class="button" href="#" onclick="return ec_all('collapse')" >Collapse All</a>
                        <a class="button" href="#" onclick="return ec_all('expand')" >Expand All</a>
                    </div>
                    <div id="table-content">
                        <?php 
                            echo $this->Session->flash();
                        ?>
                        <?php 
                        $i = 1;
                        foreach($values as $value): ?>
                        <?php
                            echo $this->element('question',array('order_class'=>$i,'question'=>$value,'survey_id'=>$survey_id));
                        ?>
                        <?php $i++;
                        endforeach;
                        ?>
                    </div>
                <div class="clear"></div>
                </div>
            </td>
            <td id="tbl-border-right"></td>
	</tr>
	<tr>
            <th class="sized bottomleft"></th>
            <td id="tbl-border-bottom">&nbsp;</td>
            <th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>

</div>
<script type="text/javascript">
jQuery(document).ready(function(){
            jQuery("#surveyMenu").addClass('current');
});
</script>